var detail = {
    /**
     * 表情相关
     */
    face : function(){
        $('body').delegate('.reply', {
            click : function(event){
                $(this).next('.comment_body').toggle();
                event.preventDefault();
            }
        });
        $('.btn-submit').bind({
            click : function(){
                var content = $(this).parents('form').find('.content').val();
                $('#result').html(content).parseEmotion();
            }
        });
        $(".comment-list").parseEmotion();
        $('.comment-main').on("click",".face-show",function(event){
            if(! $('#sinaEmotion').is(':visible')){
                $(this).sinaEmotion();
                event.stopPropagation();
            }
        });
    },
    /**
     * 评论相关
     */
    comment:function(){
        //评论
        var _this = this,_selfDom = null;
        $("#comment-btn").click(function(){
            if(!hasLogin()){
                layer.alert("请先登录");
                return ;
            }
            if($("#content").val() == ""){
                layer.alert("请输入内容");
                return;
            }else {
                var params = {
                    postId : $("#content").attr("data-postId"),
                    uuid   : getCookie("uuid"),
                    content : $("#content").val(),
                    name : getCookie("name"),
                    type : 1
                }
                $.post("/comment",params,function(res){
                    if("0" == res.code){
                        layer.alert("评论成功");
                        _this.getCommentList();//重新获取列表
                    }else{
                        layer.alert(res.message);
                    }
                })
            }
        })
        //回复 框展示隐藏
        $(".comment-main").on("click",".reply-btn",function(event){
            _selfDom = $(this);
            var container = $(this).parents(".item");
            if(container.find(".reply-box").length > 0 && container.find(".reply-box").attr("data-expand") == "0"){
                //展开
                container.find(".reply-box").attr("data-expand","1");
                container.find(".reply-box").show();
            }else if(container.find(".reply-box").length > 0 && container.find(".reply-box").attr("data-expand") == "1"){
                //折起
                container.find(".reply-box").attr("data-expand","0");
                container.find(".reply-box").hide();
            }else{
                //添加
                container.append(
                    '<form class="comment reply-box" data-expand="1">'+
                        '<div class="comment_body">'+
                            '<textarea class="content" ></textarea>'+
                            '<div class="btns">'+
                                '<i class="face-show fa fa-smile-o"></i>'+
                                '<input class="btn btn-primary btn-submit comment-btn" type="button" value="回复">'+
                                '<div class="clearfix"></div>'+
                            '</div>'+
                        '</div>'+
                    '</form>'
                )
            }
        })
        //回复评论
        $(".comment-main").on("click",".comment-btn",function(event){
            if(!hasLogin()){
                layer.alert("请先登录");
                return ;
            }
            var container = $(this).parents(".item");
            if(container.find(".content").val() == ""){
                layer.alert("请输入评论内容");
                return;
            }
            var params = {
                postId           : $("#content").attr("data-postId"),
                uuid             : getCookie("uuid"),
                content          : container.find(".content").val(),
                name             : getCookie("name"),
                type             : 2,
                replyName        : _selfDom.parents(".data-box").eq(0).attr("data-name"), //原评论人
                replyUuid        : _selfDom.parents(".data-box").eq(0).attr("data-id") //原评论人uuid
            }
            $.post("/comment",params,function(res){
                if("0" == res.code){
                    layer.alert("回复成功");
                    _this.getCommentList();//重新获取列表
                }else{
                    layer.alert(res.message);
                }
            })
        })
    },
    /**
     * 获取评论列表
     */
    getCommentList:function(){
        var _this = this;
        var postId = $("#content").attr("data-postId");
        $.get("/commentList",{postId : postId},function(res){
            if("0" == res.code){
                _this.renderCommentList(res.commentList);
            }
        })
    },
    /**
     * 生成评论列表
     * @param {*} list 
     */
    renderCommentList:function(list){
        var _this = this;
        var commentList = $(".cm-list").empty();
        list.forEach(function(item) {
            if(item.type == "1"){
                //评论
                var replyList = _this.getReply(list,item.commentUuid);
                commentList.append(
                    '<div class="item">'+
                        '<img src="images/2296.jpg" alt="" width="45" height="45" class="header">'+
                        '<div data-name="' + item.name + '" data-id="' + item.commentUuid + '" class="data-box">'+
                            '<p><span class="name" data-name="' + item.name + '" data-replyuuid="'+item.commentUuid+'">'+ item.name +' ：</span><span class="comment-txt">'+ item.content  +'</span></p>'+
                            '<p><span class="time">'+ item.createTime +' </span><a href="javascript:void(0)" class="reply-btn">回复</a></p>'+
                            replyList +
                        '</div>'+
                    '</div>'
                )
               
            }
        });
        $(".total-comment").text(list.length);
        $(".comment-list").parseEmotion();
    },
    /**
     * 获取回复列表
     */
    getReply:function(data,id){
        var str = "",_this = this;
        data.forEach(function(item){
            if(item.replyUuid == id && item.type == "2"){
                //说明这条评论是它的子评论
                str += '<div data-name="' + item.name + '" data-id="' + item.commentUuid + '" class="data-box">'+
                            '<p><span class="name">'+ item.name +' </span><span>回复 <span class="reply-name">'+item.replyName+'</span>：</span><span class="comment-txt">'+ item.content  +'</span></p>'+
                            '<p><span class="time">'+ item.createTime +' </span><a href="javascript:void(0)" class="reply-btn">回复</a></p>'+
                            _this.getReply(data,item.commentUuid) +
                        "</div>";
            }
        })
        return str;
    },
    //点赞
    like : function(){
        $(".like").click(function(){
            if(!getCookie("uuid")){
                layer.alert("请先登录");
                return ;
            }else{
                var data = {
                    uuid   : getCookie("uuid"),
                    postId : $("#content").attr("data-postId"),
                    token  : getCookie("token")
                }
                $.post('/post/like' ,data ,function(res){
                    if( "0" == res.code ){
                        if(res.flag ){
                            $(".like").addClass("active");
                        }else{
                            $(".like").removeClass("active");
                        }
                    }
                })
            }
        })
    },
    /**
     * 初始化
     */
    init:function(){
        this.getCommentList();
        this.face();
        this.comment();
        this.like();
    }
}
$(function(){
    detail.init();
})